package top.xuziyi.crawler.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.xuziyi.common.pojo.StockLb;
import top.xuziyi.common.pojo.StockZs;
import top.xuziyi.crawler.dao.StockZsDao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Service
public class StockZsService {

    @Autowired
    private StockZsDao stockZsDao;

    @PersistenceContext
    protected EntityManager em;

    @Transactional
    public void batchSave(List<StockZs> list) {
        for (int i = 0; i < list.size(); i++) {
            if (stockZsDao.findBySymbol(list.get(i).getSymbol()) == 0) {
                em.persist(list.get(i));
            }
            if (i % 200 == 0) {
                em.flush();
                em.clear();
            }
        }
    }

    public List<StockZs> findAll() {
        return stockZsDao.findAll();
    }

    public void updateStatus(String symbol, String status) {
        stockZsDao.updateStatus(status, symbol);
    }
}
